<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar bg-white" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('manage.company.bluevauthapply')}}
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar+5 + 'px 10px 0px 10px'}]"></view>
		<view class="data-box">
			<view class="cu-bar bg-white search">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="$t('global.plsuserid')" v-model="searchText"  @input="searchTag"/>
					<view class="search-close" v-if="searchText!=''" @tap="searchClose()"><text class="cuIcon-close"></text></view>
				</view>
			</view>
			<view class="search-type grid col-2">
				<view class="search-type-box" @tap="toType(0)" :class="type==0?'active':''">
					<text>{{$t('global.approval')}}</text>
				</view>
				<view class="search-type-box" @tap="toType(1)" :class="type==1?'active':''">
					<text>{{$t('global.passed')}}</text>
				</view>
			</view>
			<view class="identify-list">
				<view class="no-data" v-if="identifyList.length==0">
					<text class="cuIcon-text"></text>{{$t('common.nodata')}}
				</view>
				<view class="identify-item-box" v-for="(item,index) in identifyList" :key="index">
					<view class="identify-box-concent">
						<view class="identify-box-value">
							{{$t('manage.company.applyreason')}}:<text>{{item.entname}}</text>
						</view>
						<view class="identify-box-value">
							{{$t('manage.company.applytitle')}}:<text>{{item.name}}</text>
						</view> 
					</view>
					<view class="identify-box-user">
						<text class="text-blue">
							<text class="cuIcon-people">{{item.userJson.name}}</text>
						</text>
						<text class="identify-box-btn">
							<block v-if="item.identifyStatus==0">
								<text class="text-blue margin-right-sm" @tap="systemReview(item.uid)">{{$t('global.pass')}}</text>
								<text class="text-orange margin-right-sm" @tap="modifyAndApprove(item)">{{$t('manage.company.changetitlepass')}}</text>
								<text class="text-red" @tap="toDelete(item.uid,0)">{{$t('global.nopass')}}</text>
							</block>
							<block v-else>
								<text class="text-orange margin-right-sm" @tap="modifyTitle(item)">{{$t('manage.company.changetitle')}}</text>
								<text class="text-red" @tap="toDelete(item.uid,1)">{{$t('manage.company.revokeauth')}}</text>
							</block>
						</text>
					</view>
				</view>
				<view class="load-more" @tap="loadMore" v-if="identifyList.length>0">
					<text>{{moreText}}</text>
				</view>
			</view>
		</view>
		<!-- 修改称号弹窗 -->
		<view class="cu-modal" :class="showModifyModal?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="" :style="{'background-color':isDark?'#2a2a2a':'#ffffff'}">
				<view class="cu-bar" :style="{'background-color':isDark?'#2a2a2a':'#ffffff', 'border-bottom': isDark?'1px solid #444':'1px solid #f0f0f0'}">
					<view class="content" :style="{'color':isDark?'#ffffff':'#333333'}">
						{{modifyType === 'approve' ? $t('manage.company.changetitlepass') : $t('manage.company.changetitle')}}
					</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close" :style="{'color':isDark?'#ffffff':'#333333'}"></text>
					</view>
				</view>
				<view class="padding" :style="{'background-color':isDark?'#2a2a2a':'#ffffff'}">
					<view class="cu-form-group" :style="{'border-bottom': isDark?'1px solid #444':'1px solid #f0f0f0'}">
						<view class="title" :style="{'color':isDark?'#ffffff':'#333333'}">{{$t('manage.company.newtitle')}}</view>
						<input placeholder="$t('manage.company.plsnewtitle')" v-model="newName" :placeholder-style="isDark?'color:#999':'color:#999'" :style="{'color':isDark?'#ffffff':'#333333'}" />
					</view>
					<view class="padding flex flex-direction">
						<button class="cu-btn bg-blue lg" @tap="submitModify" :disabled="!newName.trim()">
							{{modifyType === 'approve' ? $t('manage.company.reviseapprove') : $t('manage.company.sureeditpass')}}
						</button>
						<button class="cu-btn line-gray lg margin-top-sm" @tap="hideModal">{{$t('common.cancel')}}</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				userInfo:"",
				token:'',
				moreText:$t('common.loadmoretxt'),
				searchText:'',
				type:0,
				
				identifyList:[],
				
				// 分页相关
				page: 1,
				isLoad: 0,
				// 修改称号相关
				showModifyModal: false,
				modifyType: '', // 'approve' 或 'modify'
				currentItem: null,
				newName: '',
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getIdentifyList(false);
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}
			that.getCacheInfo();
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(localStorage.getItem('token')){
				that.token=localStorage.getItem('token');
				that.getIdentifyList(false);
			}
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			getCacheInfo(){
				var that = this;
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					that.uid=userInfo.uid;
					that.userInfo = userInfo;
				}
			},
			toType(i){
				var that = this;
				that.type=i;
				that.page=1;
				that.moreText = that.$t('common.loadmoretxt');
				that.isLoad=0;
				that.identifyList = [];
				that.getIdentifyList(false);
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getIdentifyList(true);
			},
			searchTag(){
				var that = this;
				var searchText = that.searchText;
				that.page=1;
				that.getIdentifyList();
			},
			getIdentifyList(isPage){
				var that = this;
				var data = {
					"identifyStatus":that.type,
				}
				if(that.searchText!=""){
					data.uid = that.searchText;
				}
				var page = that.page;
				if(isPage){
					page++;
				}
				that.$Net.request({
					url: that.$API.companyList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":8,
						"page":page,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.stopPullDownRefresh()
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								if(isPage){
									that.page++;
									that.identifyList = that.identifyList.concat(list);
								}else{
									that.identifyList = list;
								}
							}else{
								that.moreText = that.$t('manage.company.nomorecontent');
								if(!isPage){
									that.identifyList = list;
								}
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.stopPullDownRefresh()
						that.moreText = that.$t('common.loadmoretxt');
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			systemReview(uid){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.company.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            
				            that.$Net.request({
				            	url: that.$API.systemIdentifyCompany(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
										if(localStorage.getItem('userinfo')){
											var userInfo = JSON.parse(localStorage.getItem('userinfo'));
											var uid=userInfo.uid;
										}
										uni.request({
											url:that.$API.SPgetcompany(),
											method:'GET',
											data:{
												uid:uid,
											},
											dataType:"json",
											success(res) {
												
											},
											fail() {
												setTimeout(function () {
													uni.hideLoading();
												}, 1000);
												uni.showToast({
													title: that.$t('common.networkfail'),
													icon: 'none'
												})
											}
										})
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getIdentifyList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        }
				    }
				});
			},
			autoReview(uid){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.company.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.identifyCompany(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getIdentifyList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        }
				    }
				});
			},
			toDelete(uid,type){
				var that = this;
				var token = "";
				
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.company.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.removeCompany(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
										if(type==1){
											uni.request({
												url:that.$API.SPgetuncompany(),
												method:'GET',
												data:{
													uid:uid,
												},
												dataType:"json",
												success(res) {
												},
												fail() {
													setTimeout(function () {
														uni.hideLoading();
													}, 1000);
													uni.showToast({
														title: that.$t('common.networkfail'),
														icon: 'none'
													})
												}
											})
										}
										that.page=1;
										that.moreText = that.$t('common.loadmoretxt');
										that.isLoad=0;
										that.getIdentifyList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        }
				    }
				});
			},
			// 修改称号相关方法
			modifyAndApprove(item) {
				this.modifyType = 'approve';
				this.currentItem = item;
				this.newName = item.name || '';
				this.showModifyModal = true;
			},
			
			modifyTitle(item) {
				this.modifyType = 'modify';
				this.currentItem = item;
				this.newName = item.name || '';
				this.showModifyModal = true;
			},
			
			hideModal() {
				this.showModifyModal = false;
				this.currentItem = null;
				this.newName = '';
				this.modifyType = '';
			},
			
			submitModify() {
				const that = this;
				if (!that.newName.trim()) {
					uni.showToast({
						title: that.$t('manage.company.plsentetitle'),
						icon: 'none'
					});
					return;
				}
				let token = '';
				if (localStorage.getItem('userinfo')) {
					const userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token = userInfo.token;
				}
				const data = {
					uid: that.currentItem.uid,
					name: that.newName.trim(),
					type: that.modifyType,
					token: token
				};
				// 关闭弹窗
				that.hideModal();
				uni.showModal({
					title: that.$t('manage.company.sureperformopt'),
					success: function (res) {
						if (res.confirm) {
							uni.showLoading({
								title: that.$t('common.loadmore')
							});
							that.$Net.request({
								url: that.$API.systemIdentifyCompany(),
								data: data,
								header: {
									'Content-Type': 'application/x-www-form-urlencoded'
								},
								method: "get",
								dataType: 'json',
								success: function(res) {
									setTimeout(function () {
										uni.hideLoading();
									}, 1000);
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
									if (res.data.code == 1) {
										// 如果是approve操作，需要调用相关接口
										if (that.modifyType === 'approve') {
											if (localStorage.getItem('userinfo')) {
												const userInfo = JSON.parse(localStorage.getItem('userinfo'));
												const uid = userInfo.uid;
											}
											uni.request({
												url: that.$API.SPgetcompany(),
												method: 'GET',
												data: {
													uid: that.currentItem.uid,
												},
												dataType: "json",
												success(res) {
													
												},
												fail() {
													setTimeout(function () {
														uni.hideLoading();
													}, 1000);
													uni.showToast({
														title: that.$t('common.networkfail'),
														icon: 'none'
													});
												}
											});
										}
										// 刷新列表
										that.page = 1;
										that.moreText = that.$t('common.loadmoretxt');
										that.isLoad = 0;
										that.getIdentifyList(false);
										// 关闭弹窗
										that.hideModal();
									}
								},
								fail: function(res) {
									setTimeout(function () {
										uni.hideLoading();
									}, 1000);
									uni.showToast({
										title: that.$t('common.networkfail'),
										icon: 'none'
									});
								}
							});
						}
					}
				});
			},
		}
	}
</script>

<style>
	.text-orange {
		color: #f37b1d !important;
	}
	
	.cu-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 9999;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		opacity: 0;
		visibility: hidden;
		transition: all 0.3s;
	}
	
	.cu-modal.show {
		opacity: 1;
		visibility: visible;
	}
	
	.cu-dialog {
		width: 90%;
		max-width: 600rpx;
		border-radius: 20rpx;
		overflow: hidden;
		transform: scale(0.9);
		transition: all 0.3s;
	}
	
	.cu-modal.show .cu-dialog {
		transform: scale(1);
	}
	
	.cu-form-group {
		display: flex;
		align-items: center;
		padding: 30rpx 0;
	}
	
	.cu-form-group .title {
		width: 150rpx;
		font-size: 30rpx;
	}
	
	.cu-form-group input {
		flex: 1;
		font-size: 30rpx;
		padding: 0 20rpx;
	}
</style>
